package com.hg.library.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by admin on 2017/2/22.
 */

public class DeleteButton extends Button {

    private float mPadding = 5;

    private float mRotate = 0;
    private float mTranslate;

    private boolean isStart;

    private List<Float> mRotates = new ArrayList<>();

    Iterator<Float> iterator;

    public DeleteButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        setLayerType(LAYER_TYPE_HARDWARE, null);
        setBackgroundColor(getResources().getColor(android.R.color.transparent));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawARGB(0,255,255,255);

        Paint mPaint = new Paint();
        mPaint.setColor(Color.argb(255, 65, 72, 75));
        mPaint.setAntiAlias(true);

        {
            int saveLayer = canvas.saveLayer(0, 0, getWidth(), getHeight(), null, Canvas.ALL_SAVE_FLAG);
            canvas.scale(0.9f, 1, getWidth() / 2, 0);
            canvas.translate(0, -5);
            canvas.drawRoundRect(5 + mPadding, 0, getWidth() - mPadding - 5, getHeight() - mPadding, 10, 10, mPaint);

            mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
            canvas.drawRect(5 + mPadding, 0, getWidth() - mPadding - 3, 18 + mPadding + 3, mPaint);

            mPaint.setXfermode(null);
            canvas.restoreToCount(saveLayer);
        }

        {
            int saveLayer = canvas.saveLayer(0, 0, getWidth(), getHeight(), null, Canvas.ALL_SAVE_FLAG);
            canvas.scale(0.9f, 1, getWidth() / 2, 0);
            canvas.translate(0, -5);

            if (isStart) {
                if (mTranslate > -5 && (iterator == null || iterator.hasNext())) {
                    mTranslate -= 0.4f;
                } else {
                    if (iterator == null) {
                        iterator = mRotates.iterator();
                    }
                    if (iterator.hasNext()) {
                        mRotate = iterator.next();
                    } else {
                        if (mTranslate <= 0){
                            mTranslate += 0.4f;
                        }else {
                            mRotate = 0;
                            mTranslate = 0;
                            mRotates.clear();
                            isStart = false;
                            iterator = null;
                            invalidate();
                        }
                    }
                }
            }

            canvas.translate(0, mTranslate);
            canvas.rotate(mRotate, getWidth() / 2, getHeight() / 2);

            Path path = new Path();
            path.moveTo(0 + mPadding, 13 + mPadding);
            path.lineTo(0 + mPadding, 18 + mPadding);
            path.lineTo(getWidth() - mPadding, 18 + mPadding);
            path.lineTo(getWidth() - mPadding, 13 + mPadding);
            path.lineTo(getWidth() - 10 - mPadding, 13 + mPadding);
            path.lineTo(getWidth() - 13 - mPadding, 10 + mPadding);
            path.lineTo(13 + mPadding, 10 + mPadding);
            path.lineTo(10 + mPadding, 13 + mPadding);
            path.close();

            canvas.drawPath(path, mPaint);
            canvas.restoreToCount(saveLayer);

            if (isStart) {
                invalidate();
            }
        }


    }

    public void click() {
        if (!isStart) {

            addFPS();

            isStart = true;

            invalidate();
        }
    }

    private void addFPS(){
        mRotates.add(10f);
        mRotates.add(7f);
        mRotates.add(5f);
        mRotates.add(2f);
        mRotates.add(0f);
        mRotates.add(-2f);
        mRotates.add(-5f);
        mRotates.add(-7f);
        mRotates.add(-10f);
        mRotates.add(-7f);
        mRotates.add(-5f);
        mRotates.add(-2f);
        mRotates.add(0f);
        mRotates.add(2f);
        mRotates.add(5f);
        mRotates.add(7f);
        mRotates.add(10f);
        mRotates.add(7f);
        mRotates.add(5f);
        mRotates.add(2f);
        mRotates.add(0f);

        mRotates.add(-2f);
        mRotates.add(-5f);
        mRotates.add(-7f);
        mRotates.add(-10f);
        mRotates.add(-7f);
        mRotates.add(-5f);
        mRotates.add(-2f);
        mRotates.add(0f);
    }

}
